package com.eugenema.service;

import com.eugenema.demand.Demand;
import com.eugenema.util.Annotation.DemandBean;
import com.eugenema.util.FileUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * <p>标题：注解扫描服务</p>
 * <p>
 * 功能：扫描指定包路径下的所有注解
 * </p>
 * <p>其他说明：</p>
 * <p>作者：马寅广</p>
 * <p>审核：</p>
 * <p>重构：</p>
 * <p>创建日期：2023/12/31 13:27</p>
 */
public class ScanAnnotationService {
    /** 日志 */
    private static Logger logger = LoggerFactory.getLogger(ScanAnnotationService.class);
    /** 需求注解包路径 */
    private static final String[] DEMAND_PACKAGE_LIST = {"com.eugenema.demand.impl.DemandF2Schedule", "com.eugenema.demand.impl.DemandF5CheckMatch", "com.eugenema.demand.impl.DemandF5FillMatchResult", "com.eugenema.demand.impl.DemandF5RedGemBook", "com.eugenema.demand.impl.DemandF5Schedule"};
    /** 需求注解包路径 */
    private static final String DEMAND_PACKAGE_PATH = "com.eugenema.demand.impl";

    /**
     * 扫描需求注解
     *
     * @author 马寅广
     * @date 2023/12/31 13:51
     *
     **/
    public void scanDemand() {
        List<Class<?>> scanResult = FileUtil.getAllClasses(DEMAND_PACKAGE_PATH);

        // 初始化需求池容量
        Demand.demandPool = new Demand[scanResult.size()];
        // 实例化使用了 DemandBean 的类并添加到需求池中
        for (Class<?> clazz : scanResult) {
            if (clazz.isAnnotationPresent(DemandBean.class)) {
                try {
                    DemandBean annotation = clazz.getAnnotation(DemandBean.class);
                    Demand demand = (Demand) clazz.newInstance();
                    demand.setKey(annotation.key());
                    demand.setTitle(annotation.title());
                    demand.setNotice(annotation.notice());
                    demand.setOrder(annotation.order());
                    Demand.demandPool[demand.getOrder()] = demand;
                } catch (Exception e) {
                    logger.error("实例化需求类失败", e);
                }
            }
        }
    }
}
